﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<!-- 关联信息区域 -->
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>关联信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_HrStaffNeedId_FormItem" prop="Entity.HrStaffNeedId">
								<template #label>
									<WtmLabelTooltip label="用人申请单号" tooltip="关联的用人申请单" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.HrStaffNeedId" disabled clearable class="width100">
									<el-option
										v-for="item in stateData.AllSysHrStaffNeedRequestBills"
										:key="item.Value"
										:value="item.Value"
										:label="item.Text"
									></el-option
								></el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<!-- 基本信息区域 -->
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>
					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_HireNumber_FormItem" prop="Entity.HireNumber">
								<template #label>
									<WtmLabelTooltip label="招聘人数" tooltip="本岗位计划招聘人数" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.HireNumber" :min="1" disabled clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ExpectedDate_FormItem" prop="Entity.ExpectedDate">
								<template #label>
									<WtmLabelTooltip label="到岗时间" tooltip="期望人员到岗的时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.ExpectedDate" type="date" disabled clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="Entity_ApplyReason_FormItem" prop="Entity.ApplyReason">
								<template #label>
									<WtmLabelTooltip label="申请原因" tooltip="提出招聘需求的原因" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.ApplyReason" type="textarea" :rows="3" disabled clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="Entity_JobRequirement_FormItem" prop="Entity.JobRequirement">
								<template #label>
									<WtmLabelTooltip label="岗位要求" tooltip="岗位的具体要求" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.JobRequirement" type="textarea" :rows="3" disabled clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<!-- 任职要求区域 -->
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>任职要求</span>
					</div>
					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_EducationRequirement_FormItem" prop="Entity.EducationRequirement">
								<template #label>
									<WtmLabelTooltip label="学历要求" tooltip="最低学历要求" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.EducationRequirement" disabled clearable class="width100">
									<el-option key="小学" value="小学" label="小学"></el-option>
									<el-option key="初中" value="初中" label="初中"></el-option>
									<el-option key="高中" value="高中" label="高中"></el-option>
									<el-option key="中专" value="中专" label="中专"></el-option>
									<el-option key="大专" value="大专" label="大专"></el-option>
									<el-option key="本科" value="本科" label="本科"></el-option>
									<el-option key="研究生" value="研究生" label="研究生"></el-option>
									<el-option key="硕士" value="硕士" label="硕士"></el-option>
									<el-option key="博士" value="博士" label="博士"></el-option>
									<el-option key="博士后" value="博士后" label="博士后"></el-option>
									<el-option key="博士研究生" value="博士研究生" label="博士研究生"></el-option>
									<el-option key="其他" value="其他" label="其他"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_MajorRequirement_FormItem" prop="Entity.MajorRequirement">
								<template #label>
									<WtmLabelTooltip label="专业要求" tooltip="所需专业方向" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.MajorRequirement" disabled clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_GenderRequirement_FormItem" prop="Entity.GenderRequirement">
								<template #label>
									<WtmLabelTooltip label="性别要求" tooltip="岗位对性别的要求" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.GenderRequirement" disabled clearable class="width100">
									<el-option key="男" value="男" label="男"></el-option>
									<el-option key="女" value="女" label="女"></el-option>
									<el-option key="其他" value="其他" label="其他"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_AgeRequirement_FormItem" prop="Entity.AgeRequirement">
								<template #label>
									<WtmLabelTooltip label="年龄要求" tooltip="岗位对年龄的要求" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.AgeRequirement" placeholder="如：25-35岁" disabled clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="24" class="mb20">
							<el-form-item ref="Entity_OtherRequirement_FormItem" prop="Entity.OtherRequirement">
								<template #label>
									<WtmLabelTooltip label="其他要求" tooltip="除上述外的其他要求" />
								</template>
								<el-input
									v-model="stateData.vmModel.Entity.OtherRequirement"
									type="textarea"
									:rows="2"
									disabled
									clearable
									class="width100"
								></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<!-- 工作信息区域 -->
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>工作信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<WtmOrgSelector v-model="stateData.vmModel.Entity.OrgId" label="公司" prop="Entity.OrgId" :disabled="true" />
						</el-col>
					</el-row>
					<wtm-dept-selector
						v-model="stateData.vmModel.Entity.GroupId"
						prop="Entity.GroupId"
						label="所属部门"
						:show-position="true"
						v-model:position-id="stateData.vmModel.Entity.JobPositionId"
						position-prop="Entity.JobPositionId"
						position-label="工作岗位"
						:use-row="true"
						:dept-col="12"
						:position-col="12"
						:disabled="true"
						:position-disabled="true"
						:org-id="stateData.vmModel.Entity.OrgId"
					/>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.edit;false">
import { reactive, ref, getCurrentInstance, onMounted, nextTick, type ComponentInternalInstance } from 'vue';
import ElMessage from 'element-plus/es/components/message/index';
import { SysHrStaffNeedRequestBillEntryApi as api } from '/@/views/hr/syshrstaffneedrequestbillentry/api';
import other from '/@/utils/other';
import { ISysHrStaffNeedRequestBillEntryVm } from '/@/views/hr/syshrstaffneedrequestbillentry/api/interface';
import { IViewModel } from '/@/types/common';

// 常量定义

const BUTTON_TEXTS = {
	CLOSE: '关闭',
} as const;

const API_ENDPOINTS = {
	BILLS: '/api/SysHrStaffNeedRequestBillEntry/GetSysHrStaffNeedRequestBills',
} as const;

const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();

// 初始化表单数据
const stateData = reactive<IViewModel<ISysHrStaffNeedRequestBillEntryVm>>({
	vmModel: {
		Entity: {
			ID: null,
			HireNumber: 0,
			ApplyReason: null,
			ExpectedDate: null,
			JobRequirement: null,
			EducationRequirement: null,
			MajorRequirement: null,
			GenderRequirement: null,
			AgeRequirement: null,
			OtherRequirement: null,
			GroupId: null,
			JobPositionId: null,
			HrStaffNeedId: null,
			OrgId: null,
		} as any,
	},
	AllSysHrStaffNeedRequestBills: [],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 初始化页面数据
const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	const routeId = window.location.search.match(/[?&]id=([^&]*)/)?.[1];

	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	} else if (routeId) {
		stateData.vmModel.Entity.ID = routeId as any;
	}
};

// 加载选择列表数据
const loadSelectListData = async () => {
	try {
		// 加载选择列表数据
		const bills = await other.getSelectList(API_ENDPOINTS.BILLS, [], false);
		stateData.AllSysHrStaffNeedRequestBills = bills;
	} catch (error) {
		// 静默处理错误
	}
};

// 加载实体数据
const loadEntityData = async () => {
	if (!stateData.vmModel.Entity.ID) {
		return;
	}

	try {
		const response = await api().get(stateData.vmModel.Entity.ID);
		other.setValue(stateData.vmModel, response);
	} catch (error) {
		ElMessage.error('加载数据失败，请重试');
		throw error;
	}
};

// 页面加载时
onMounted(async () => {
	loading.value = true;

	try {
		// 首先初始化页面数据（获取ID）
		initializePageData();

		// 并行加载选择列表数据和实体数据
		await Promise.all([loadSelectListData(), loadEntityData()]);
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];

	emit('updateHeaderButtons', headerButtons);
};
</script>
